package com.aso114.project.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BasesAdapter;
import com.aso114.project.bean.JingPinBean;
import com.aso114.project.commonview.MyListView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SelectedTopicPayAdapter extends BasesAdapter<SelectedTopicPayAdapter.ViewHolder> {

    private OnItemClickListener mListener;
    ArrayList<JingPinBean> bean = new ArrayList<>();
    private Context context;

    public SelectedTopicPayAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(getInflater().inflate(R.layout.adapter_selected_topic_pay, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if ("1".equals(bean.get(position).getTypeId())) {
            holder.payIcon.setImageResource(R.mipmap.jxtk_ji_ic);
        } else if ("2".equals(bean.get(position).getTypeId())) {
            holder.payIcon.setImageResource(R.mipmap.jxtk_ya_ic);
        }
        holder.payTitle.setText(bean.get(position).getTitle());
        holder.payEndPuy.setText(bean.get(position).getOrderCount() + " 人已购买");
        holder.payOriginalPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        holder.payOriginalPrice.setText("￥" + bean.get(position).getTwoPrice());
        holder.payLastPrice.setText("￥" + bean.get(position).getOnePrice());

        SelectedTopicPayItemAdapter adapter = new SelectedTopicPayItemAdapter(context, bean.get(position).getPaperList());
        holder.payListView.setAdapter(adapter);
        if (bean.get(position).getIsLock() == 1) {//已付费
            bean.get(position).setCheckEnabled(true);
            holder.payCheck.setImageResource(R.mipmap.buy_uncheck_gray_iccopy);
        } else {
            bean.get(position).setCheckEnabled(false);
            holder.payCheck.setImageResource(R.mipmap.buy_uncheck_iccopy);
        }
        holder.payCheckLy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!bean.get(position).isCheckEnabled()) // 是否可点击（已付费不可点）
                    if (bean.get(position).isCheck()) {
                        holder.payCheck.setImageResource(R.mipmap.buy_uncheck_iccopy);
                        bean.get(position).setCheck(false);
                        bean.get(position).setSelect(false);
                        if (mListener != null) {
                            mListener.onItemClick();
                        }
                    } else {
                        holder.payCheck.setImageResource(R.mipmap.buy_check_ic);
                        bean.get(position).setCheck(true);
                        bean.get(position).setSelect(true);
                        if (mListener != null) {
                            mListener.onItemClick();
                        }
                    }
            }
        });
    }

    @Override
    public int getItemCount() {
        return bean.size();
    }

    public void setData(ArrayList<JingPinBean> bean) {
        this.bean = bean;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.pay_check)
        ImageView payCheck;
        @BindView(R.id.pay_icon)
        ImageView payIcon;
        @BindView(R.id.pay_title)
        TextView payTitle;
        @BindView(R.id.pay_end_puy)
        TextView payEndPuy;
        @BindView(R.id.pay_original_price)
        TextView payOriginalPrice;
        @BindView(R.id.pay_last_price)
        TextView payLastPrice;
        @BindView(R.id.pay_listView)
        MyListView payListView;
        @BindView(R.id.pay_check_ly)
        ConstraintLayout payCheckLy;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick();
    }
}
